﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using LibraryDS;
using LibraryUS;
using IPCommon;
using LibraryDS.CDBNames;

namespace LibraryMain
{
    public partial class f003_V_DM_CUON_SACH_DE : Form
    {
        public f003_V_DM_CUON_SACH_DE()
        {
            InitializeComponent();
            format_controls();
        }

        #region Public Interface
        public void display()
        {
            this.ShowDialog();
        }
        public void display_for_update(US_V_DM_CUON_SACH ip_us_v_dm_cuon_sach)
        {
            m_us_v_dm_cuon_sach = ip_us_v_dm_cuon_sach;
            m_e_form_mode = DataEntryFormMode.UpdateDataState;
            this.ShowDialog();
        }
        public void display_for_insert()
        {
            m_e_form_mode = DataEntryFormMode.InsertDataState;
            this.ShowDialog();
        }

        #endregion

        #region Members

        DataEntryFormMode m_e_form_mode;
        US_V_DM_CUON_SACH m_us_v_dm_cuon_sach = new US_V_DM_CUON_SACH();
        US_DM_CUON_SACH m_us_dm_cuon_sach = new US_DM_CUON_SACH();
        #endregion

        #region Data Structure
        #endregion

        #region Private Method

        private void enable_combobox(bool ip_b)
        {
            m_cbo_ten_tua_sach.Enabled = ip_b;
        }
        private void load_data_to_combobox()
        {
            DS_V_DM_CUON_SACH v_ds_v_dm_cuon_sach = new DS_V_DM_CUON_SACH();
            m_us_v_dm_cuon_sach.FillDataset(v_ds_v_dm_cuon_sach);

            m_cbo_ten_tua_sach.DataSource = v_ds_v_dm_cuon_sach.V_DM_CUON_SACH;
            m_cbo_ten_tua_sach.DisplayMember = V_DM_CUON_SACH.TEN_TUA_SACH;
            m_cbo_ten_tua_sach.ValueMember = V_DM_CUON_SACH.ID_DAU_SACH;

            m_cbo_ngon_ngu.DataSource = v_ds_v_dm_cuon_sach.V_DM_CUON_SACH;
            m_cbo_ngon_ngu.DisplayMember = V_DM_CUON_SACH.NGON_NGU;
            m_cbo_ngon_ngu.ValueMember = V_DM_CUON_SACH.ID_DAU_SACH;

            m_cbo_bia.DataSource = v_ds_v_dm_cuon_sach.V_DM_CUON_SACH;
            m_cbo_bia.DisplayMember = V_DM_CUON_SACH.TEN_BIA;
            m_cbo_bia.ValueMember = V_DM_CUON_SACH.ID_DAU_SACH;

            m_cbo_ten_tac_gia.DataSource = v_ds_v_dm_cuon_sach.V_DM_CUON_SACH;
            m_cbo_ten_tac_gia.DisplayMember = V_DM_CUON_SACH.TEN_TAC_GIA;
            m_cbo_ten_tac_gia.ValueMember = V_DM_CUON_SACH.ID_DAU_SACH;
        }

        private void format_controls()
        {
            CControlFormat.setFormStyle(this);
            set_define_events();
        }
        private void us_object_to_form()
        {
            load_data_to_combobox();
            m_cbo_ten_tua_sach.SelectedValue = m_us_v_dm_cuon_sach.dcID_DAU_SACH;
            m_cbo_ngon_ngu.SelectedValue = m_us_v_dm_cuon_sach.dcID_DAU_SACH;
            m_cbo_bia.SelectedValue = m_us_v_dm_cuon_sach.dcID_DAU_SACH;
            m_cbo_ten_tac_gia.SelectedValue = m_us_v_dm_cuon_sach.dcID_DAU_SACH;
            m_txt_ma_cuon_sach.Text = m_us_v_dm_cuon_sach.strMA_CUON_SACH;
            if (m_us_v_dm_cuon_sach.strTINH_TRANG.Equals("Y") || m_us_v_dm_cuon_sach.strTINH_TRANG.Equals("y"))
            {
                m_ckb_con_sach.Checked = true;
            }
            else m_ckb_con_sach.Checked = false;
            m_txt_ghi_chu.Text = m_us_v_dm_cuon_sach.strGHI_CHU;
        }
        private void form_to_us_object()
        {
            m_us_dm_cuon_sach.dcID = m_us_v_dm_cuon_sach.dcID;
            m_us_dm_cuon_sach.dcID_DAU_SACH = CIPConvert.ToDecimal(m_cbo_ten_tac_gia.SelectedValue);
            m_us_dm_cuon_sach.strMA_CUON_SACH = m_txt_ma_cuon_sach.Text;
            if (m_ckb_con_sach.Checked == true)
            {
                m_us_dm_cuon_sach.strTINH_TRANG = "Y";
            }
            else m_us_dm_cuon_sach.strTINH_TRANG = "N";
            m_us_dm_cuon_sach.strGHI_CHU = m_txt_ghi_chu.Text;
        }
        private void set_inital_form_load()
        {
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    enable_combobox(false);
                    us_object_to_form();
                    break;
                case DataEntryFormMode.InsertDataState:
                    load_data_to_combobox();
                    enable_combobox(true);
                    break;
                default:
                    break;
            }
        }
        private void set_define_events()
        {
            this.Load += new EventHandler(f002_V_DM_DM_TU_DIEN_DE_Load);
            m_cmd_update.Click += new EventHandler(m_cmd_update_Click);
            m_cmd_exit.Click += new EventHandler(m_cmd_exit_Click);
        }
        private bool check_validate_data_is_ok()
        {
            if (!CValidateTextBox.IsValid(m_txt_ma_cuon_sach, DataType.StringType, allowNull.NO, true))
            {
                return false;
            }
          return true;
        }
        private void save_data()
        {
            if (check_validate_data_is_ok() == false) return;
            form_to_us_object();
            switch (m_e_form_mode)
            {
                case DataEntryFormMode.UpdateDataState:
                    m_us_dm_cuon_sach.Update();
                    break;
                case DataEntryFormMode.InsertDataState:
                    m_us_dm_cuon_sach.Insert();
                    break;
                default:
                    break;
            }
            BaseMessages.MsgBox_Infor("Dữ liệu đã được cập nhât!");
            this.Close();
        }

        #endregion

        #region Event
        private void f002_V_DM_DM_TU_DIEN_DE_Load(object sender, EventArgs e)
        {
            try
            {
                set_inital_form_load();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_update_Click(object sender, EventArgs e)
        {
            try
            {
                save_data();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        private void m_cmd_exit_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                CSystemLog_301.ExceptionHandle(v_e);
            }
        }
        #endregion
    }
}
